from enum import Enum
from typing import Optional, List, Union
from pydantic import BaseModel, Field, root_validator
from fastapi import Body, Header, Cookie, Path, Query


class Language(BaseModel):
    """
    语言表
    """
    id: int = Field(..., description='ID', gt=0)
    name: str = Field(..., description='说明', example='简体中文')
    code: str = Field(..., description='地区代码', example='zh_CN')

    @root_validator(pre=True)
    def create(cls, values):
        """ 传入res.lang的记录 """
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['name'] = mdl.name
            values['code'] = mdl.code
        return values


class TranslationProgram(BaseModel):
    """
    翻译的项目
    """
    id: int
    name: str

    @root_validator(pre=True)
    def create(cls, values):
        """ 传入pe.translation.program的记录 """
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['name'] = mdl.name
        return values


class TranslationClient(BaseModel):
    """
    翻译的终端
    """
    id: int
    name: str

    @root_validator(pre=True)
    def create(cls, values):
        """ 传入pe.translation.client的记录 """
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['name'] = mdl.name
        return values


class TranslationLine(BaseModel):
    """
    翻译对照明细
    """
    index: str = Field(None, description='索引')
    chinese: Optional[str] = Field(None, description='简体中文')
    english: Optional[str] = Field(None, description='英文(US)')
    spanish: Optional[str] = Field(None, description='西语(Español)')
    catalan: Optional[str] = Field(None, description='加泰罗尼亚语(Català)')

    @root_validator(pre=True)
    def create(cls, values):
        """ 传入pe.translation.table的记录 """
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['index'] = mdl.index_id.code
            values['chinese'] = mdl.chinese if mdl.chinese else None
            values['english'] = mdl.english if mdl.chinese else None
            values['spanish'] = mdl.spanish if mdl.spanish else None
            values['catalan'] = mdl.catalan if mdl.catalan else None
        return values


class TranslationTable(BaseModel):
    """
    翻译对照表
    """

    program: TranslationProgram = Field(..., description='项目')
    client: TranslationClient = Field(..., description='终端')
    line: List[TranslationLine] = Field([], description='翻译对照明细')
